<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>build - Hello wasm-pack!</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="shortcut icon" href="../favicon.png">
        <link rel="stylesheet" href="../css/variables.css">
        <link rel="stylesheet" href="../css/general.css">
        <link rel="stylesheet" href="../css/chrome.css">
        <link rel="stylesheet" href="../css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
        <link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
        <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../highlight.css">
        <link rel="stylesheet" href="../tomorrow-night.css">
        <link rel="stylesheet" href="../ayu-highlight.css">

        <!-- Custom theme stylesheets -->
        

        
    </head>
    <body class="light">
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "../";
            var default_theme = "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } 
            if (theme === null || theme === undefined) { theme = default_theme; }
            document.body.className = theme;
            document.querySelector('html').className = theme + ' js';
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <ol class="chapter"><li><a href="../introduction.html"><strong aria-hidden="true">1.</strong> Introduction</a></li><li><a href="../quickstart.html"><strong aria-hidden="true">2.</strong> Quickstart</a></li><li><a href="../prerequisites/index.html"><strong aria-hidden="true">3.</strong> Prerequisites</a></li><li><ol class="section"><li><a href="../prerequisites/npm.html"><strong aria-hidden="true">3.1.</strong> npm (optional)</a></li><li><a href="../prerequisites/considerations.html"><strong aria-hidden="true">3.2.</strong> considerations</a></li><li><a href="../prerequisites/non-rustup-setups.html"><strong aria-hidden="true">3.3.</strong> Non-rustup setups</a></li></ol></li><li><a href="../commands/index.html"><strong aria-hidden="true">4.</strong> Commands</a></li><li><ol class="section"><li><a href="../commands/new.html"><strong aria-hidden="true">4.1.</strong> new</a></li><li><a href="../commands/build.html" class="active"><strong aria-hidden="true">4.2.</strong> build</a></li><li><a href="../commands/test.html"><strong aria-hidden="true">4.3.</strong> test</a></li><li><a href="../commands/pack-and-publish.html"><strong aria-hidden="true">4.4.</strong> pack and publish</a></li><li><a href="../commands/init.html"><strong aria-hidden="true">4.5.</strong> init (DEPRECATED)</a></li></ol></li><li><a href="../tutorials/index.html"><strong aria-hidden="true">5.</strong> Tutorials</a></li><li><ol class="section"><li><a href="../tutorials/hybrid-applications-with-webpack/index.html"><strong aria-hidden="true">5.1.</strong> Hybrid applications with Webpack</a></li><li><ol class="section"><li><a href="../tutorials/hybrid-applications-with-webpack/getting-started.html"><strong aria-hidden="true">5.1.1.</strong> Getting started</a></li><li><a href="../tutorials/hybrid-applications-with-webpack/using-your-library.html"><strong aria-hidden="true">5.1.2.</strong> Using your library</a></li></ol></li><li><a href="../tutorials/npm-browser-packages/index.html"><strong aria-hidden="true">5.2.</strong> npm browser packages</a></li><li><ol class="section"><li><a href="../tutorials/npm-browser-packages/getting-started.html"><strong aria-hidden="true">5.2.1.</strong> Getting started</a></li><li><ol class="section"><li><a href="../tutorials/npm-browser-packages/getting-started/manual-setup.html"><strong aria-hidden="true">5.2.1.1.</strong> Manual Setup</a></li></ol></li><li><a href="../tutorials/npm-browser-packages/template-deep-dive/index.html"><strong aria-hidden="true">5.2.2.</strong> Template deep dive</a></li><li><ol class="section"><li><a href="../tutorials/npm-browser-packages/template-deep-dive/cargo-toml.html"><strong aria-hidden="true">5.2.2.1.</strong> Cargo.toml</a></li><li><a href="../tutorials/npm-browser-packages/template-deep-dive/src-lib-rs.html"><strong aria-hidden="true">5.2.2.2.</strong> src/lib.rs</a></li><li><a href="../tutorials/npm-browser-packages/template-deep-dive/src-utils-rs.html"><strong aria-hidden="true">5.2.2.3.</strong> src/utils.rs</a></li><li><a href="../tutorials/npm-browser-packages/template-deep-dive/wee_alloc.html"><strong aria-hidden="true">5.2.2.4.</strong> wee_alloc</a></li><li><a href="../tutorials/npm-browser-packages/template-deep-dive/tests-web-rs.html"><strong aria-hidden="true">5.2.2.5.</strong> tests/web.rs</a></li></ol></li><li><a href="../tutorials/npm-browser-packages/building-your-project.html"><strong aria-hidden="true">5.2.3.</strong> Building your project</a></li><li><a href="../tutorials/npm-browser-packages/testing-your-project.html"><strong aria-hidden="true">5.2.4.</strong> Testing your project</a></li><li><a href="../tutorials/npm-browser-packages/packaging-and-publishing.html"><strong aria-hidden="true">5.2.5.</strong> Packaging and publishing</a></li><li><a href="../tutorials/npm-browser-packages/using-your-library.html"><strong aria-hidden="true">5.2.6.</strong> Using your library</a></li></ol></li></ol></li><li><a href="../cargo-toml-configuration.html"><strong aria-hidden="true">6.</strong> Cargo.toml Configuration</a></li><li><a href="../contributing.html"><strong aria-hidden="true">7.</strong> Contributing</a></li></ol>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <style>
  header.warning {
    background-color: rgb(242, 222, 222);
    border-bottom-color: rgb(238, 211, 215);
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    border-bottom-style: solid;
    border-bottom-width: 0.666667px;
    border-image-outset: 0 0 0 0;
    border-image-repeat: stretch stretch;
    border-image-slice: 100% 100% 100% 100%;
    border-image-source: none;
    border-image-width: 1 1 1 1;
    border-left-color: rgb(238, 211, 215);
    border-left-style: solid;
    border-left-width: 0.666667px;
    border-right-color: rgb(238, 211, 215);
    border-right-style: solid;
    border-right-width: 0.666667px;
    border-top-color: rgb(238, 211, 215);
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    border-top-style: solid;
    border-top-width: 0.666667px;
    color: rgb(185, 74, 72);
    margin-bottom: 0px;
    margin-left: 0px;
    margin-right: 0px;
    margin-top: 30px;
    padding-bottom: 8px;
    padding-left: 14px;
    padding-right: 35px;
    padding-top: 8px;
    text-align: center;
  }
</style>
<header class='warning'>
  This documentation is
  <a
  href="https://blog.rust-lang.org/inside-rust/2025/07/21/sunsetting-the-rustwasm-github-org/">no
  longer maintained at this domain</a>, and is now maintained
  <a href="https://drager.github.io/wasm-pack/">
  at drager.github.io/wasm-pack</a> instead.
</header>

                <div id="menu-bar" class="menu-bar">
                    <div id="menu-bar-sticky-container">
                        <div class="left-buttons">
                            <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                                <i class="fa fa-bars"></i>
                            </button>
                            <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                                <i class="fa fa-paint-brush"></i>
                            </button>
                            <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                                <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
                                <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                                <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                                <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                                <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                            </ul>
                            
                        </div>

                        <h1 class="menu-title">Hello wasm-pack!</h1> 

                        <div class="right-buttons">
                            <a href="../print.html" title="Print this book" aria-label="Print this book">
                                <i id="print-button" class="fa fa-print"></i>
                            </a>
                            
                        </div>
                    </div>
                </div>

                

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <a class="header" href="#wasm-pack-build" id="wasm-pack-build"><h1>wasm-pack build</h1></a>
<p>The <code>wasm-pack build</code> command creates the files necessary for JavaScript
interoperability and for publishing a package to npm. This involves compiling
your code to wasm and generating a pkg folder. This pkg folder will contain the
wasm binary, a JS wrapper file, your <code>README</code>, and a <code>package.json</code> file.</p>
<p>The <code>pkg</code> directory is automatically <code>.gitignore</code>d by default, since it contains
build artifacts which are not intended to be checked into version
control.<sup><a href="#footnote-0">0</a></sup></p>
<a class="header" href="#path" id="path"><h2>Path</h2></a>
<p>The <code>wasm-pack build</code> command can be given an optional path argument, e.g.:</p>
<pre><code>wasm-pack build examples/js-hello-world
</code></pre>
<p>This path should point to a directory that contains a <code>Cargo.toml</code> file. If no
path is given, the <code>build</code> command will run in the current directory.</p>
<a class="header" href="#output-directory" id="output-directory"><h2>Output Directory</h2></a>
<p>By default, <code>wasm-pack</code> will generate a directory for its build output called <code>pkg</code>.
If you'd like to customize this you can use the <code>--out-dir</code> flag.</p>
<pre><code>wasm-pack build --out-dir out
</code></pre>
<p>The above command will put your build artifacts in a directory called <code>out</code>, instead
of the default <code>pkg</code>.</p>
<a class="header" href="#generated-file-names" id="generated-file-names"><h2>Generated file names</h2></a>
<p>Flag <code>--out-name</code> sets the prefix for output file names. If not provided, package name is used instead.</p>
<p>Usage examples, assuming our crate is named <code>dom</code>:</p>
<pre><code>wasm-pack build
# will produce files
# dom.d.ts  dom.js  dom_bg.d.ts  dom_bg.wasm  package.json  README.md

wasm-pack build --out-name index
# will produce files
# index.d.ts  index.js  index_bg.d.ts  index_bg.wasm  package.json  README.md
</code></pre>
<a class="header" href="#profile" id="profile"><h2>Profile</h2></a>
<p>The <code>build</code> command accepts an optional profile argument: one of <code>--dev</code>,
<code>--profiling</code>, or <code>--release</code>. If none is supplied, then <code>--release</code> is used.</p>
<p>This controls whether debug assertions are enabled, debug info is generated, and
which (if any) optimizations are enabled.</p>
<table><thead><tr><th> Profile       </th><th> Debug Assertions </th><th> Debug Info </th><th> Optimizations </th><th> Notes                                 </th></tr></thead><tbody>
<tr><td> <code>--dev</code>       </td><td> Yes              </td><td> Yes        </td><td> No            </td><td> Useful for development and debugging. </td></tr>
<tr><td> <code>--profiling</code> </td><td> No               </td><td> Yes        </td><td> Yes           </td><td> Useful when profiling and investigating performance issues. </td></tr>
<tr><td> <code>--release</code>   </td><td> No               </td><td> No         </td><td> Yes           </td><td> Useful for shipping to production.    </td></tr>
</tbody></table>
<p>The <code>--dev</code> profile will build the output package using cargo's <a href="https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections">default
non-release profile</a>. Building this way is
faster but applies few optimizations to the output, and enables debug assertions
and other runtime correctness checks. The <code>--profiling</code> and <code>--release</code> profiles
use cargo's release profile, but the former enables debug info as well, which
helps when investigating performance issues in a profiler.</p>
<p>The exact meaning of the profile flags may evolve as the platform matures.</p>
<a class="header" href="#target" id="target"><h2>Target</h2></a>
<p>The <code>build</code> command accepts a <code>--target</code> argument. This will customize the JS
that is emitted and how the WebAssembly files are instantiated and loaded. For
more documentation on the various strategies here, see the <a href="https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html">documentation on
using the compiled output</a>.</p>
<pre><code>wasm-pack build --target nodejs
</code></pre>
<table><thead><tr><th> Option    </th><th> Usage </th><th> Description                                                                                                     </th></tr></thead><tbody>
<tr><td> <em>not specified</em> or <code>bundler</code> </td><td> <a href="https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#bundlers">Bundler</a> </td><td> Outputs JS that is suitable for interoperation with a Bundler like Webpack. You'll <code>import</code> the JS and the <code>module</code> key is specified in <code>package.json</code>. <code>sideEffects: false</code> is by default. </td></tr>
<tr><td> <code>nodejs</code>  </td><td> <a href="https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#nodejs">Node.js</a> </td><td> Outputs JS that uses CommonJS modules, for use with a <code>require</code> statement. <code>main</code> key in <code>package.json</code>. </td></tr>
<tr><td> <code>web</code> </td><td> <a href="https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#without-a-bundler">Native in browser</a> </td><td> Outputs JS that can be natively imported as an ES module in a browser, but the WebAssembly must be manually instantiated and loaded. </td></tr>
<tr><td> <code>no-modules</code> </td><td> <a href="https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#without-a-bundler">Native in browser</a> </td><td> Same as <code>web</code>, except the JS is included on a page and modifies global state, and doesn't support as many <code>wasm-bindgen</code> features as <code>web</code> </td></tr>
<tr><td> <code>deno</code> </td><td> <a href="https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#deno">Deno</a> </td><td> Outputs JS that can be natively imported as an ES module in deno. </td></tr>
</tbody></table>
<a class="header" href="#scope" id="scope"><h2>Scope</h2></a>
<p>The <code>build</code> command also accepts an optional <code>--scope</code> argument. This will scope
your package name, which is useful if your package name might conflict with
something in the public registry. For example:</p>
<pre><code>wasm-pack build examples/js-hello-world --scope test
</code></pre>
<p>This command would create a <code>package.json</code> file for a package called
<code>@test/js-hello-world</code>. For more information about scoping, you can refer to
the npm documentation <a href="https://docs.npmjs.com/misc/scope">here</a>.</p>
<a class="header" href="#mode" id="mode"><h2>Mode</h2></a>
<p>The <code>build</code> command accepts an optional <code>--mode</code> argument.</p>
<pre><code>wasm-pack build examples/js-hello-world --mode no-install
</code></pre>
<table><thead><tr><th> Option        </th><th> Description                                                                              </th></tr></thead><tbody>
<tr><td> <code>no-install</code>  </td><td> <code>wasm-pack build</code> implicitly and create wasm binding without installing <code>wasm-bindgen</code>.  </td></tr>
<tr><td> <code>normal</code>      </td><td> do all the stuffs of <code>no-install</code> with installed <code>wasm-bindgen</code>.                         </td></tr>
</tbody></table>
<a class="header" href="#extra-options" id="extra-options"><h2>Extra options</h2></a>
<p>The <code>build</code> command can pass extra options straight to <code>cargo build</code> even if
they are not supported in wasm-pack. To use them simply add the extra arguments
at the very end of your command, just as you would for <code>cargo build</code>. For
example, to build the previous example using cargo's offline feature:</p>
<pre><code>wasm-pack build examples/js-hello-world --mode no-install -- --offline
</code></pre>
<hr style="font-size: 1.5em; margin-top: 2.5em"/>
<p><sup id="footnote-0">0</sup> If you need to include additional assets in the pkg
directory and your NPM package, we intend to have a solution for your use case
soon. <a href="#wasm-pack-build">↩</a></p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                        
                            <a rel="prev" href="../commands/new.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>
                        

                        
                            <a rel="next" href="../commands/test.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>
                        

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                
                    <a href="../commands/new.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                

                
                    <a href="../commands/test.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
                
            </nav>

        </div>

        

        

        

        

        <script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="../book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->
        

        

    </body>
</html>
